﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;

/// <summary>
/// Representation of the container for widget
/// </summary>
public partial class WidgetContainer : System.Web.UI.UserControl
{
    /// <summary>
    /// Gets or sets the widget.
    /// </summary>
    /// <value>The widget.</value>
    public WidgetBase Widget { get; set; }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        PanelWidgetContent.Controls.Add(Widget);
        Widget.LoadWidget();
        if (!Widget.IsEditable)
        {
            ImageButtonEdit.Visible = false;
        }
        this.DivOperation.Visible = false;
        Widget.OnOperation += new WidgetBase.OperationDelegate(OperationOccured);
        Widget.OnParametrizedOperation += new WidgetBase.ParametrizedOperationDelegate(ParametrizedOperationOccured);
        Widget.EditShowed += new EventHandler(ShowEditIcons);
        Widget.EditHidden += new EventHandler(HideEditIcons);
        LiteralTitle.Text = (string)GetGlobalResourceObject("WidgetTypeTitle", Widget.Name);
        ImageButtonToNewTab.PostBackUrl = "~/Default.aspx?tabID=-1&widgetTypeID="+Widget.TypeID+"&tabName="+ Server.UrlDecode(Widget.Title);

		spanMinimize.InnerHtml = String.Format("<img src=\"Resources/Pictures/application_get.png\" style=\"cursor:pointer;\" onclick=\"minimizeWidget('{0}', '{1}', '{2}', true)\" alt=\"{3}\" title=\"{3}\" />", this.PanelContent.ClientID, this.spanMinimize.ClientID, this.spanMaximize.ClientID, (string)GetLocalResourceObject("Minimize"));
		spanMaximize.InnerHtml = String.Format("<img src=\"Resources/Pictures/widget_maximize.png\" style=\"cursor:pointer;\" onclick=\"minimizeWidget('{0}', '{1}', '{2}', false)\" alt=\"{3}\" title=\"{3}\" />", this.PanelContent.ClientID, this.spanMinimize.ClientID, this.spanMaximize.ClientID, (string)GetLocalResourceObject("Maximize"));
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="WidgetContainer"/> class.
    /// </summary>
    public WidgetContainer()
    {

    }

    /// <summary>
    /// Initializes a new instance of the <see cref="WidgetContainer"/> class.
    /// </summary>
    /// <param name="widget">The widget.</param>
    public WidgetContainer(WidgetBase widget)
    {
        this.Widget = widget;
    }

    /// <summary>
    /// Shows message, when some operation on widget occurs.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="parameters">The parameters.</param>
    protected void ParametrizedOperationOccured(WidgetBase.MessageType type, string message, params object[] parameters)
    {
        this.DivOperation.Visible = true;
        string resourceFile = String.Format("{0}Messages", type.ToString());
        string messageText = (string)GetGlobalResourceObject(resourceFile, message);
        this.LocalizeOperation.Text = String.Format(messageText, parameters).Replace("\n", "<br />");
        string cssClass = String.Format("{0}Message", type.ToString());
        this.DivOperation.Attributes["class"] = cssClass;      
    }

    /// <summary>
    /// Shows message, when some operation on widget occurs.
    /// </summary>
    /// <param name="message">The message.</param>
    protected void OperationOccured(WidgetBase.MessageType type, string message)
    {
        ParametrizedOperationOccured(type, message, new object[] { });
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ShowEditIcons(object sender, EventArgs e)
    {
        ImageButtonEdit.Visible = false;
        ImageButtonCloseEdit.Visible = true;
        this.UpdatePanelHeader.Update();
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void HideEditIcons(object sender, EventArgs e)
    {
        ImageButtonEdit.Visible = true;
        ImageButtonCloseEdit.Visible = false;
        this.UpdatePanelHeader.Update();
    }

    /// <summary>
    /// Edits the widget.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void EditWidget(object sender, EventArgs e)
    {
        Widget.ShowEdit();
        ImageButtonEdit.Visible = false;
        ImageButtonCloseEdit.Visible = true;
        this.UpdatePanelHeader.Update();
        this.UpdatePanelContent.Update();
    }

    /// <summary>
    /// Closes the edit widget.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void CloseEditWidget(object sender, EventArgs e)
    {
        Widget.HideEdit();
        ImageButtonEdit.Visible = true;
        ImageButtonCloseEdit.Visible = false;
        this.UpdatePanelHeader.Update();
        this.UpdatePanelContent.Update();
    }
   
    /// <summary>
    /// Deletes widget
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
    protected void ImageButtonDelete_Click(object sender, ImageClickEventArgs e)
    {
        DAOWidget.DeleteWidget(DAOWidget.GetWidgetByID(this.Widget.ID));
		Response.Redirect(this.Page.Request.Url.PathAndQuery);
    }
}
